#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>

typedef void(*func_t)();

#define N 3
func_t tasks[N] = {NULL};


void 

enum 
{
  Success = 0,
  Div_zero,
  Mod_zero,
};

int exit_code=Success;

const char* CodeToErrString(int code)
{
  switch(code)
  {
    case Success:
      return "Success";
    case Div_zero:
      return "div zero";
    case Mod_zero:
      return "mod zero";
    default:
      return "unknown error!";
  }
}

int Div(int x,int y)
{
  if(0 == y)
  {
    exit_code = Div_zero;
    return -1;
  }
  else 
  {
    exit_code = Success;
    return x/y;
  }

}
void ChildRun()
{
  int cnt=5;
  while(1)
  {
    printf("I am child process,pid:%d,ppid:%d,cnt:%d\n",getpid(),getppid(),cnt);
    sleep(1);
  }
}


int main()
{
  printf("I am father process,pid:%d,ppid:%d\n",getpid(),getppid());
  pid_t id = fork();
  if(id == 0)
  {
    //child
    ChildRun();
    printf("child quit...\n");
    exit(123);
  }
  sleep(10);
  ////father
  //pid_t rid = wait(NULL);
  int status = 0;
  pid_t rid = waitpid(id,&status,0);
  if(rid > 0)
  {
    printf("wait success,rid:%d\n",rid);
  }
  else 
  {
    printf("wait failed!\n");
  }
  sleep(3);
  printf("status:%d,father quit...,child quit code:%d,child quit signal:%d\n",status,(status>>8)&0xFF,status & 0x7F);

  //int* p=NULL;
 // while(1)
 // {
 //   printf("I am process,pid:%d\n",getpid());
 //   sleep(1);
 //   exit(123);
 //   //*p = 1;
 // }

 // return 0;
  //int result = Div(10,0);
  //printf("result:%d [%s]\n",result,CodeToErrString(exit_code));
  //result = Div(10,100);
  //printf("result:%d [%s]\n",result,CodeToErrString(exit_code));
  return exit_code ;
//  int errcode;
//  for(errcode = 0;errcode<=255;errcode++)
//  {
//    printf("%d:%s\n",errcode,strerror(errcode));
//  }
//  printf("I am a process,pid:%d,ppid:%d\n",getpid(),getppid());
//  sleep(2);
 // return exit_code ;
}
